﻿//using System;
//using System.Collections.Generic;
//using FluentNHibernate;
//using FluentNHibernate.Automapping;
//using FluentNHibernate.Cfg;
//using FluentNHibernate.Cfg.Db;
//using FluentNHibernate.Conventions;
//using FluentNHibernate.Conventions.Inspections;

//using Insesoft.Entities;
//using Insesoft.Entities.Mappings.Fluent;
//using NHibernate;
//using NHibernate.Cfg;

//namespace InseSoft.UIControllers
//{
//    public static class NHibernateHelper
//    {
//        #region Properties: Public

//        private static ISessionFactory sessionFactory;

//        #endregion

//        #region Properties: Public

//        private static ISessionFactory SessionFactory
//        {
//            get { return sessionFactory ?? (sessionFactory = CreateSessionFactory()); }
//        }

//        #endregion

//        #region Methods: Public

//        public static ISession OpenSession()
//        {
//            return SessionFactory.OpenSession()
//                .SetBatchSize(20);
//        }

//        public static void Initialize()
//        {
//            CreateSessionFactory();
//        }

//        #endregion

//        #region Methods: Non-Public

//        private static ISessionFactory CreateSessionFactory()
//        {
//            var configuration = CreateConfiguration();
//            return configuration.BuildSessionFactory();
//        }

//        public static Configuration CreateConfiguration()
//        {
//            return Fluently.Configure()
//                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("INSE")))
//                //.Mappings(m => m.HbmMappings.AddFromAssemblyOf<EntidadBase>())

//                //.Mappings(m => m.FluentMappings.AddFromAssemblyOf<EntidadBase>()
//                //                .Conventions.Add<ForeingKeyNamingConvention>()
//                //                .Conventions.Add<ManyToManyTableNamingConvention>())
//                .Mappings(m => m.FluentMappings.Add<OrdenMap>()
//                                .Conventions.Add<ForeingKeyNamingConvention>()
//                                .Conventions.Add<ManyToManyTableNamingConvention>())
//                .Mappings(m => m.AutoMappings.Add(CreateMappings()
//                                    .Conventions.Add<ForeingKeyNamingConvention>()
//                                    .Conventions.Add<ManyToManyTableNamingConvention>()))
//                .BuildConfiguration();
//        }

//        private static AutoPersistenceModel CreateMappings()
//        {
//            var exclusions = new List<string> { "SolicitudCotizacionReport", "EntidadBase", "Orden"};
//            return AutoMap
//                .Assemblies()
//                .AddMappingsFromAssemblyOf<EntidadBase>()
//                .Where(t => t.Namespace == "Insesoft.Entities"
//                        && t.IsClass
//                        && !exclusions.Contains(t.Name));
//        }

//        #endregion
//    }

//    public class ForeingKeyNamingConvention : ForeignKeyConvention
//    {
//        protected override string GetKeyName(Member property, Type type)
//        {
//            if (property == null)
//                // This line will disable `KeyColumnConvention`
//                return type.Name + "Id";

//            // This line has no effect when `ReferenceKeyConvention` is enabled.
//            return property.Name + "Id";
//        }
//    }

//    public class ManyToManyTableNamingConvention : ManyToManyTableNameConvention
//    {
//        protected override string GetBiDirectionalTableName(IManyToManyCollectionInspector collection,
//            IManyToManyCollectionInspector otherSide)
//        {
//            return collection.EntityType.Name + "_" + otherSide.EntityType.Name;
//        }

//        protected override string GetUniDirectionalTableName(IManyToManyCollectionInspector collection)
//        {
//            return collection.EntityType.Name + "_" + collection.ChildType.Name;
//        }
//    }
//}
